package android.tusnotas.services;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;

public class WebCheckerManager{

	private static Activity activity;

	public WebCheckerManager (Activity activitie){
		activity = activitie;
	}

	public void startWebChecker(){
		if(!isWebCheckerStarted()){
			Intent webCheckerService = new Intent(activity.getApplicationContext(), WebChecker.class);
			activity.startService(webCheckerService);
		}
	}

	public void stopWebChecker(){
		if (isWebCheckerStarted()){
			Intent webCheckerService = new Intent(activity.getApplicationContext(), WebChecker.class);
			activity.stopService(webCheckerService);
		}
	}

	public boolean isWebCheckerStarted(){

		ActivityManager am = (ActivityManager) activity.getSystemService(Activity.ACTIVITY_SERVICE);
		List<RunningServiceInfo> rs = am.getRunningServices(50);
		for (RunningServiceInfo rsi : rs) {

			if (rsi.service.getClassName().equals(WebChecker.class.getName())){
				return Boolean.TRUE;
			}
		}

		return Boolean.FALSE;
	}

}
